<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
        <el-form-item label="编号" prop="id">
          <el-input v-model="queryParams.id"></el-input>
        </el-form-item>
        <el-form-item label="渠道" prop="channel">
          <el-select v-model="queryParams.channel" clearable placeholder="请选择">
            <el-option label="USDT通道" value="USDT" />
            <el-option label="BTC通道" value="BTC" />
            <el-option label="ETH通道" value="ETH" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" clearable placeholder="请选择">
            <el-option label="初始" value="INIT" />
            <el-option label="完成" value="SUCCESS" />
            <el-option label="失败" value="FAIL" />
            <el-option label="处理中" value="ING" />
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="queryParams.type" clearable placeholder="请选择">
            <el-option label="充值" value="RECHARGE" />
            <el-option label="提现" value="CASH" />
            <el-option label="上分" value="UP" />
            <el-option label="下分" value="DOWN" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
          >新增</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row> -->
      <el-table v-loading="loading" :data="walletRechargeCashList" @selection-change="handleSelectionChange">
        <el-table-column label="编号" align="center" prop="id" show-overflow-tooltip />
        <el-table-column label="邮箱" align="center" prop="mail" />
        <el-table-column label="金额" align="center" prop="amount" width="80px" />
        <el-table-column label="余额" align="center" prop="balance" width="80px" />
        <el-table-column label="类型" align="center" prop="type" />
        <el-table-column label="渠道" align="center" prop="channel" />
        <el-table-column label="状态" align="center" prop="statusLabel">
          <template #default="scope">
            <el-tag v-if="scope.row.status === '0'" type="info">{{ scope.row.statusLabel }}</el-tag>
            <el-tag v-else-if="scope.row.status === '1'" type="success">{{ scope.row.statusLabel }}</el-tag>
            <el-tag v-else-if="scope.row.status === '2'" type="danger">{{ scope.row.statusLabel }}</el-tag>
            <el-tag v-else>{{ scope.row.statusLabel }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="提现地址" align="center" prop="cashAddress" />
        <el-table-column label="充值图片" align="center" prop="imgUrl" min-width="100px">
          <template #default="scope">
            <el-image class="czimg" fit="fill" :src="scope.row.imgUrl" />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="完成时间" align="center" prop="successTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.successTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="90">
          <template #default="scope">
            <div v-if="scope.row.status === '0'">
              <el-button link size="small" type="primary" @click="handleConfirm(scope.row)"
                v-hasPermi="['system:walletRechargeCash:confirm']">确认
              </el-button>
              <el-button link size="small" type="danger" @click="handleRefuse(scope.row)"
                v-hasPermi="['system:walletRechargeCash:refuse']">驳回
              </el-button>
            </div>
            <div v-if="scope.row.status === '2'">
              <span style="color: red">驳回原因：{{ scope.row.refuseReason }}</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.current" v-model:limit="queryParams.size"
        @pagination="getList" />
  
      <!-- 驳回订单填入原因对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
        <el-form ref="walletRechargeCashRef" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="驳回原因" prop="refuseReason" required>
            <el-input v-model="form.refuseReason" placeholder="请输入驳回原因" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitRefuseForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup name="WalletRechargeCash">
  import { confirmScore, refuseScore } from "@/api/boot/walletRechargeCash";
  import { walletTransList } from "@/api/charge/index.js";
  
  const { proxy } = getCurrentInstance();
  
  const walletRechargeCashList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  
  const data = reactive({
    form: {},
    queryParams: {
      current: 1,
      size: 10,
    },
    rules: {
      refuseReason: [
        { required: true, message: '驳回原因不能为空', trigger: 'blur' },
      ],
    }
  });
  
  const { queryParams, form, rules } = toRefs(data);
  
  /** 查询充值提现流水列表 */
  function getList() {
    loading.value = true;
    walletTransList(queryParams.value).then(res => {
      walletRechargeCashList.value = res.data.records;
      total.value = res.data.total;
      loading.value = false;
    })
  }
  
  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }
  
  // 表单重置
  function reset(row) {
    form.value = {
      id: row.id,
      refuseReason: '',
    };
    proxy.resetForm("walletRechargeCashRef");
  }
  
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.current = 1;
    getList();
  }
  
  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }
  
  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length !== 1;
    multiple.value = !selection.length;
  }
  
  function handleConfirm(row) {
    proxy.$modal.confirm_tg('是否确认【' + row.type + '】订单"' + row.id + '"完成', '确认订单').then(function () {
      confirmScore(row.id).then(res => {
        if (res.code === 200) {
          proxy.$modal.msgSuccess("操作成功");
          getList();
        }
      })
    }).catch(() => { })
  }
  
  function handleRefuse(row) {
    reset(row);
    title.value = '驳回订单'
    open.value = true;
  }
  
  function submitRefuseForm() {
    proxy.$refs["walletRechargeCashRef"].validate(valid => {
      if (valid) {
        refuseScore(form.value).then(res => {
          if (res.code === 200) {
            proxy.$modal.msgSuccess("驳回成功");
            open.value = false;
            getList();
          }
        })
      }
    })
  }
  
  getList();
  </script>
  <style>
  .czimg {
    width: 30px;
    height: 30px;
  }
  
  .czimg:hover {
    width: 100px;
    height: 100px;
  }
  </style>